package com.example.brainmemory;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.opengl.Visibility;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;

import com.example.brainmemory.engine.Numbers;
import com.example.brainmemory.ui.GameButton;

public class BrainMemory extends Activity implements OnClickListener {
	private final int MAX_COLUMNS = 4;
	private final int MAX_GAME_BUTTONS_ACTIVE = 5;
	private final int MAX_ROWS = 5;
	private GameButton gameButtons[][] = null;
	private ArrayList<Integer> gameNumbers = null;

	private TableRow gameRows[];
	/** Called when the activity is first created. */

	private TableLayout tl;
	private int tryCounter = 0;
	/**
	 * used to determine, whether all Animations have ended, so that the gamebuttons are ready to get pressed.
	 * TODO: not really nice ;-)
	 */
	private Animation lastAnimation = null;

	public GameButton getButtonByButtonId(int buttonId) {

		for (int i = 0; i < gameButtons.length; i++) {
			for (int j = 0; j < gameButtons[i].length; j++) {
				if (gameButtons[i][j].getButtonId() == buttonId)
					return gameButtons[i][j];
			}
		}
		return null;

	}

	public GameButton getButtonByViewId(int viewId) {

		for (int i = 0; i < gameButtons.length; i++) {
			for (int j = 0; j < gameButtons[i].length; j++) {
				if (gameButtons[i][j].getId() == viewId)
					return gameButtons[i][j];
			}
		}
		return null;

	}

	@Override
	public void onClick(View v) {
		if (isGameReady())
		{
			if (gameNumbers == null || gameNumbers.size() == 0)
				return;
	
			GameButton current = getButtonByViewId(v.getId());
			// TODO: Warum kommt hier das Falsche an
			if (current != null && gameNumbers.get(0) == current.getButtonId()) {
				gameNumbers.remove(0);
				Animation anim = AnimationUtils.loadAnimation(this,
						R.anim.game_button_pressed);
				current.showAnimated(anim, false);
			}
			tryCounter++;
			((TextView) findViewById(R.id.attempts)).setText(tryCounter + "");
			if (gameNumbers.size() == 0)
				showToast("Gewonnen");
		}
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		GameButton.buttonCount=0;
		tl = (TableLayout) findViewById(R.id.playground);
		int edgeLength = ((WindowManager) this.getSystemService(WINDOW_SERVICE))
				.getDefaultDisplay().getWidth()
				/ MAX_COLUMNS;
		gameButtons = new GameButton[MAX_ROWS][MAX_COLUMNS];
		for (int i = 0; i < gameButtons.length; i++) {
			for (int j = 0; j < gameButtons[i].length; j++) {
				gameButtons[i][j] = new GameButton(this, this, edgeLength);
			}
		}
		gameRows = new TableRow[MAX_ROWS];
		// Add new components
		for (int i = 0; i < gameButtons.length; i++) {
			gameRows[i] = new TableRow(this);
			for (int j = 0; j < gameButtons[i].length; j++) {
				// TODO: set gamebuttons non activ by default
				gameRows[i].addView(gameButtons[i][j]);
			}
			tl.addView(gameRows[i]);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.mainmenu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.quit_game:
			finish();
			return true;
		case R.id.start_game:
			startGame();
			return true;
		case R.id.back_to_start_menu:
			Intent startGameIntent = new Intent(BrainMemory.this,
					StartMenu.class);
			BrainMemory.this.startActivity(startGameIntent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	public void showToast(String msg) {
		final Toast debugMsg = Toast.makeText(this, msg, 2);
		debugMsg.show();
	}

	public void startGame() {
		gameNumbers = new Numbers(MAX_GAME_BUTTONS_ACTIVE, 1, MAX_ROWS
				* MAX_COLUMNS).getForcedRandomNumbers();

		for (int i = 0; i < gameButtons.length; i++) {
			gameRows[i] = new TableRow(this);
			for (int j = 0; j < gameButtons[i].length; j++) {
				gameButtons[i][j].setVisibility(GameButton.VISIBLE);
			}
			tl.addView(gameRows[i]);
		}
		
		for (int i = 0; i < gameButtons.length; i++) {
			Animation anim = AnimationUtils.loadAnimation(this,
					R.anim.game_button_hover);
			anim.setStartOffset(anim.getDuration() * i + 500);
			lastAnimation = anim;
			GameButton current = getButtonByButtonId(gameNumbers.get(i));
			current.showAnimated(anim, true);
		}

		tryCounter = 0;
		((TextView) findViewById(R.id.attempts)).setText(tryCounter + "");
	}
	
	/**
	 * @return whether all Animations have ended, so that the gamebuttons are ready to get pressed.
	 */
	private boolean isGameReady()
	{
		if (lastAnimation != null)
			if (lastAnimation.hasEnded())
				return true;
		return false;
	}

}